package com.swarnendupoc.main;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
 
import com.swarnendupoc.model.Employee;
import com.swarnendupoc.sessionfactory.HibernateUtilImpl;
 
public class MainClass {
 
public static void main(String[] args) {
//createEmployee();
readEmployee();
updateEmployee();
readEmployee();
deleteEmployee();
readEmployee();
}
 
 
private static void createEmployee() {
Employee emp = new Employee();
emp.setEmpName("Amit Singh");
emp.setDesignation("Managar");
 
Employee empp = new Employee();
empp.setEmpName("Vijay Singh");
empp.setDesignation("TL");
 
SessionFactory sf = HibernateUtilImpl.getSessionFacotry();
Session session = sf.openSession();
session.beginTransaction();
session.save(emp);
session.save(empp);
session.getTransaction().commit();
session.close();
 
}
 
 
private static void readEmployee() {
Employee emp1 = new Employee();
 
SessionFactory sf = HibernateUtilImpl.getSessionFacotry();
Session session = sf.openSession();
session.beginTransaction();
 
// provided primary key to get the perticular record
emp1 = (Employee) session.get(Employee.class, 1);
//System.out.println("The name of employee is "+emp1.getEmpName()+" And the designation of Emp is "
//+emp1.getDesignation());
session.getTransaction().commit();
session.close();
 
// to selector list all the record in the table using HQL
session = sf.openSession();
session.beginTransaction();
Query query = session.createQuery("from Employee");
List<Employee> list = query.list();
System.out.println("\n");
System.out.println("::::::::::Printing Emp Details:::::::::::");
System.out.println("\n");
for(Employee emp : list){
System.out.println("Emp id is "+emp.getEmpId()+" Emp Name is "+emp.getEmpName()
+" and the designation is "+emp.getDesignation());
}
session.getTransaction().commit();
session.close();
 
}
 
private static void updateEmployee() {
Employee emp1 = new Employee();
 
SessionFactory sf = HibernateUtilImpl.getSessionFacotry();
Session session = sf.openSession();
session.beginTransaction();
 
// provided primary key to get the perticular record
emp1 = (Employee) session.get(Employee.class, 1);
emp1.setDesignation("SSE");
System.out.println("Designation updated successfully");
session.getTransaction().commit();
session.close();
 
}
private static void deleteEmployee() {
SessionFactory sf = HibernateUtilImpl.getSessionFacotry();
Session session = sf.openSession();
session.beginTransaction();
// getting the object using load method instead of using get method
Employee emp = (Employee) session.load(Employee.class, 1);
session.delete(emp);
System.out.println("Employee Deleted");
session.getTransaction().commit();
session.clear();
}
 
 
 
}
